<?php

declare(strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Middleware\User;

use App\Constants\Constants;
use App\Kernel\Util\UserJwtInstance;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
 * 用户Token验证中间件，不做登陆拦截
 *
 * @author 刘兴永(aile8880@qq.com)
 * @package App\Middleware
 */
class UserNotNecessaryMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // 获取token
        $token = $request->getHeaderLine(Constants::AUTHORIZATION);

        try {
            if ( !empty($token) ) {
                UserJwtInstance::instance()->decode($token);
            }
        }
        catch (CustomException $e) { /* 不做拦截 */ }

        return $handler->handle($request);
    }
}